import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * hidden: 设置为true, 左侧菜单不会显示该项
 * alwaysShow: 如果设置为true，将始终显示根菜单,如果设置为false,只有一个子菜单时,不会显示菜单
 * name：'router-name'名称由<keep alive>使用（必须设置！！！）
 * meta : {
    roles: ['admin','editor']    角色权限控制
    title: 'title'               侧边栏显示的名字
    icon: 'svg-name'/'el-icon-x' 侧边栏图标
    breadcrumb: false            是否会出现面包屑
    activeMenu: '/example/list'  突出显示的侧边栏菜单项
  }
  * constantRoutes为所有角色都可以访问的页面
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    alwaysShow: true,
    meta: { title: '模块', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: '表格数据', icon: 'table' }
      },
      {
        path: 'demo',
        name: 'Demo',
        component: () => import('@/views/table/demo'),
        meta: { title: '测试使用', icon: 'el-icon-cherry' }
      }
    ]
  },

  // 404 页面
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // 历史模式,去除url中的#
  scrollBehavior: () => ({ y: 0 }),
  // 路由表, 如果有动态路由在此处添加
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
